package com.xiaoyu.lanling.feature.login.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.login_banner_fragment.*

class LoginBannerFragment : OnShowFragment() {

    companion object {
        val TAG = LoginBannerFragment::class.java.simpleName

        fun create(fragmentManager: FragmentManager, position: Int): OnShowFragment {
            val params = Bundle()
            params.putInt(Constant.IntentKey.KEY_POSITION, position)

            val fragment = (fragmentManager.findFragmentByTag(TAG) as? OnShowFragment)
                    ?: LoginBannerFragment()
            fragment.arguments = params
            return fragment
        }
    }

    val bannerResArray = arrayOf(R.drawable.login_banner_0_icon, R.drawable.login_banner_1_icon, R.drawable.login_banner_2_icon)
    val titleResArray = arrayOf(R.string.login_banner_title_0, R.string.login_banner_title_1, R.string.login_banner_title_2)
    val subtitleResArray = arrayOf(R.string.login_banner_subtitle_0, R.string.login_banner_subtitle_1, R.string.login_banner_subtitle_2)

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.login_banner_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initData()
    }

    private fun initData() {
        val position = arguments?.getInt(Constant.IntentKey.KEY_POSITION) ?: return
        banner.setImageResource(bannerResArray[position])
        title.setText(titleResArray[position])
        subtitle.setText(subtitleResArray[position])
    }
}